define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'bomlists/bomlist/index' + location.search,
                    add_url: 'bomlists/bomlist/add',
                    edit_url: 'bomlists/bomlist/edit',
                    del_url: 'bomlists/bomlist/del',
                    multi_url: 'bomlists/bomlist/multi',
                    table: 'bomlist',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Serial number')},
                        {field: 'p_bn', title: __('P_bn')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'products.name', title: __('Products.name')},
                        {field: 'products.spec', title: __('Products.spec')},
                        {field: 'products.access', title: __('Products.access'), searchList: {"self":__('Self'),"purchase":__('Purchase'),"outsourcing":__('Outsourcing'),"virtual":__('Virtual'),"custorm":__('Custorm')}, formatter: Table.api.formatter.normal},
                        {field: 'products.working', title: __('Products.working')},
                        {field: 'products.testfiles', title: __('Products.testfiles')},
                        {field: 'products.custorm_id', title: __('Products.custorm_id')},
                        {field: 'unit', title: __('Unit')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                searchFormVisible: true
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'bomlists/bomlist/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'bomlists/bomlist/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'bomlists/bomlist/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            $(document).on('click','.btn-ajaxss', function (event) {
                var url = $(this).attr('data-url');
                if(!url) return false;
                var msg = $(this).attr('data-title');
                var width = $(this).attr('data-width');
                var height = $(this).attr('data-height');
                var area = ['80%', '80%'];
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: area,
                    callback:function(value){
                        if(value){
                            var choose_product = document.getElementById("choose_product");
                            choose_product.innerText = value[0].name;
                            var product = document.getElementById("product");
                            product.value = value[0].bn;
                        }else{
                            Layer.alert('请选择一个产品');
                        }
                    }
                };
                parent.Fast.api.open(url,msg,options);
            });

            $(document).on('click','.btn-ajaxs', function (event) {
                var url = $(this).attr('data-url');
                if(!url) return false;
                var msg = $(this).attr('data-title');
                var width = $(this).attr('data-width');
                var height = $(this).attr('data-height');
                var area = ['80%', '80%'];
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: area,
                    callback:function(value){
                        if(value){
                            for (i = 0; i < value.length; i++) {
                                if(value[i].access == 'self' || value[i].access == 'outsourcing' || value[i].access == 'Virtual'){
                                    $("#MyPublishShow").append("<tr><td>" +
                                        "<a href='#' onclick='delete_one(this);' id='"+value[i].bn+"' class='btn btn-danger btn-del' ><i class='fa fa-trash'></i> 删除</a>"+
                                        "<a href='#' onclick='edit_one(this);' data-url='bomlists/bomlist/edit/ids/"+value[i].bn+"?type=bn' data-title='编辑' id='"+value[i].bn+"' class='btn btn-success btn-edit' ><i class='fa fa-pencil'></i> 查看Bom</a>"+
                                        "</td><td><a href='http://121.40.105.133/ERP/public/0fzYlrch6J.php/product_lists/products/edit/ids/"+value[i].id+"?dialog=1' class='btn btn-dialog'>"+value[i].bn+
                                        "</a></td><td>"+value[i].name+
                                        "</td><td>"+value[i].spec+
                                        "</td><td>"+value[i].unit+"<input type='hidden' name='products["+value[i].bn+"][unit]' value='"+value[i].unit+"'>"+
                                        "</td><td>"+value[i].weight+
                                        "</td><td>"+value[i].color+
                                        "</td><td>"+value[i].access_text+
                                        "</td><td>"+"<input type='text' name='products["+value[i].bn+"][Dosage]'>"+
                                        "</td><td>"+"<input type='text' name='products["+value[i].bn+"][comment]'>"+
                                        "</td></tr>");
                                }else{
                                    $("#MyPublishShow").append("<tr><td>" +
                                        "<a href='#' onclick='delete_one(this);' id='"+value[i].bn+"' class='btn btn-danger btn-del' ><i class='fa fa-trash'></i> 删除</a>"+
                                        "</td><td><a href='http://121.40.105.133/ERP/public/0fzYlrch6J.php/product_lists/products/edit/ids/"+value[i].id+"?dialog=1' class='btn btn-dialog'>"+value[i].bn+
                                        "</a></td><td>"+value[i].name+
                                        "</td><td>"+value[i].spec+
                                        "</td><td>"+value[i].unit+"<input type='hidden' name='products["+value[i].bn+"][unit]' value='"+value[i].unit+"'>"+
                                        "</td><td>"+value[i].weight+
                                        "</td><td>"+value[i].color+
                                        "</td><td>"+value[i].access_text+
                                        "</td><td>"+"<input type='text' name='products["+value[i].bn+"][Dosage]'>"+
                                        "</td><td>"+"<input type='text' name='products["+value[i].bn+"][comment]'>"+
                                        "</td></tr>");
                                }

                            }

                        }else{
                            Layer.alert('请选择一个产品');
                        }
                    }
                };
                parent.Fast.api.open(url,msg,options);
            });
        },
        edit: function () {
            Controller.api.bindevent();
            if(Config.row){
                var pro = Config.row;
                console.log(pro);
                for (i = 0; i <pro.length; i++) {
                    var res = pro[i];
                    if(res.product.access == '自制' || res.product.access == '委外' || res.product.access == '虚拟'){
                        $("#MyPublishShow").append("<tr><td>" +
                            "<a href='#' onclick='delete_one(this);' id='"+res.id+"' class='btn btn-danger btn-del' ><i class='fa fa-trash'></i> 删除</a>"+
                            "<a href='#' onclick='edit_one(this);' data-url='bomlists/bomlist/edit/ids/"+res.id+"/status/find' data-title='编辑' id='"+res.id+"' class='btn btn-success btn-edit' ><i class='fa fa-pencil'></i> 查看Bom</a>"+
                            "</td><td><a href='http://121.40.105.133/ERP/public/0fzYlrch6J.php/product_lists/products/edit/ids/"+res.product.id+"?dialog=1' class='btn btn-dialog'>"+res.product.bn+
                            "</a></td><td>"+res.product.name+
                            "</td><td>"+res.product.spec+
                            "</td><td>"+res.unit+"<input type='hidden' name='products["+res.product.bn+"][unit]' value='"+res.unit+"'><input type='hidden' name='products["+res.product.bn+"][id]' value='"+res.id+"'>"+
                            "</td><td>"+res.product.weight+
                            "</td><td>"+res.product.color+
                            "</td><td>"+res.product.access+
                            "</td><td>"+"<input type='text' name='products["+res.product.bn+"][Dosage]' value='"+res.Dosage+"'>"+
                            "</td><td>"+"<input type='text' name='products["+res.product.bn+"][comment]' value='"+res.comment+"'>"+
                            "</td></tr>");
                    }else{
                        $("#MyPublishShow").append("<tr><td>" +
                            "<a href='#' onclick='delete_one(this);' id='"+res.id+"' class='btn btn-danger btn-del' ><i class='fa fa-trash'></i> 删除</a>"+
                            "</td><td><a href='http://121.40.105.133/ERP/public/0fzYlrch6J.php/product_lists/products/edit/ids/"+res.product.id+"?dialog=1' class='btn btn-dialog'>"+res.product.bn+
                            "</a></td><td>"+res.product.name+
                            "</td><td>"+res.product.spec+
                            "</td><td>"+res.unit+"<input type='hidden' name='products["+res.product.bn+"][unit]' value='"+res.unit+"'><input type='hidden' name='products["+res.product.bn+"][id]' value='"+res.id+"'>"+
                            "</td><td>"+res.product.weight+
                            "</td><td>"+res.product.color+
                            "</td><td>"+res.product.access+
                            "</td><td>"+"<input type='text' name='products["+res.product.bn+"][Dosage]' value='"+res.Dosage+"'>"+
                            "</td><td>"+"<input type='text' name='products["+res.product.bn+"][comment]' value='"+res.comment+"'>"+
                            "</td></tr>");
                    }

                }

            }else{
                Layer.alert('未知错误，请联系管理员查看');
            }
            $(document).on('click','.btn-ajaxs', function (event) {
                var url = $(this).attr('data-url');
                if(!url) return false;
                var msg = $(this).attr('data-title');
                var width = $(this).attr('data-width');
                var height = $(this).attr('data-height');
                var area = ['80%', '80%'];
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: area,
                    callback:function(value){
                        if(value){
                            for (i = 0; i < value.length; i++) {
                                $("#MyPublishShow").append("<tr><td>" +
                                    "<a href='#' onclick='delete_one(this);' id='"+value[i].bn+"' name='edit' class='btn btn-danger btn-del' ><i class='fa fa-trash'></i> 删除</a>"+
                                    "</td><td><a href='http://121.40.105.133/ERP/public/0fzYlrch6J.php/product_lists/products/edit/ids/"+value[i].id+"?dialog=1' class='btn btn-dialog'>"+value[i].bn+
                                    "</a></td><td>"+value[i].name+
                                    "</td><td>"+value[i].spec+
                                    "</td><td>"+value[i].unit+"<input type='hidden' name='products[newbom]["+value[i].bn+"][unit]' value='"+value[i].unit+"'>"+
                                    "</td><td>"+value[i].weight+
                                    "</td><td>"+value[i].color+
                                    "</td><td>"+value[i].access_text+
                                    "</td><td>"+"<input type='text' name='products[newbom]["+value[i].bn+"][Dosage]'>"+
                                    "</td><td>"+"<input type='text' name='products[newbom]["+value[i].bn+"][comment]'>"+
                                    "</td></tr>");
                            }

                        }else{
                            Layer.alert('请选择一个产品');
                        }
                    }
                };
                parent.Fast.api.open(url,msg,options);
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});